//
//  ChoiceDayViewController.swift
//  SwiftClockDemo
//
//  Created by gwj on 10/16/15.
//  Copyright (c) 2015 gwj. All rights reserved.
//

import UIKit

class ChoiceDayViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet var choiceTableView: UITableView!
    
    var delegate: ChoiceDayDelegate?
    
//    var choiceDayArray :[Bool] = NSArray() as! [Bool]
    var choiceDayArray :NSMutableArray = NSMutableArray()
    var arrayWeek :NSArray!
    
    //MARK: LifeCircle
    override func viewDidLoad() {
        super.viewDidLoad()
        choiceTableView.delegate = self;
        choiceTableView.dataSource = self;
        arrayWeek = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
        // Do any additional setup after loading the view.
    }

    //MARK: TableViewDataSource
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayWeek.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView .dequeueReusableCellWithIdentifier("ChoiceDayTableViewCell", forIndexPath: indexPath)as! ChoiceDayTableViewCell
        
        cell.dayLabel.text = arrayWeek.objectAtIndex(indexPath.row) as? String
        return cell
    }
    
    //MARK: TableViewDelegate
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        if cell?.accessoryType == UITableViewCellAccessoryType.None {
            cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
        }
        choiceDayArray.addObject(arrayWeek.objectAtIndex(indexPath.row))
        print(choiceDayArray.count, terminator: "")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    


    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        
        self.delegate?.choiceDay!(self.choiceDayArray)
    }

}

@objc protocol ChoiceDayDelegate {
    
    optional func choiceDay(_: NSArray)
}
